package com.yssoft.datasource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

/**
 * @author 鲁菊平
 * @time 2025/11/05 下午 13:52
 * @ 监管企业管理 数据源动态配置
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    @Override
    protected Object determineCurrentLookupKey() {
        String dataSource = DataSourceContextHolder.getDataSource();
        // 如果数据源为空，使用默认的 MySQL
        if (dataSource == null || dataSource.isEmpty()) {
            return DataSourceContextHolder.MYSQL;
        }
        return dataSource;

    }

    public static void setDataSource(String dataSource) {
        contextHolder.set(dataSource);
    }

    public static String getDataSource() {
        return contextHolder.get();
    }

    public static void clearDataSource() {
        contextHolder.remove();
    }
}